<?php

namespace app\adminapi\controller\v1\setting;

use app\adminapi\controller\AuthController;
use app\services\other\AgreementServices;
use think\facade\App;

class SystemAgreement extends AuthController
{
    /**
     * 构造方法
     * SystemCity constructor.
     * @param App $app
     * @param AgreementServices $services
     */
    public function __construct(App $app, AgreementServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 获取列表
     * @return mixed
     */
    public function index()
    {
        $where = $this->request->getMore([
            ["title",""]
        ]);
        if(!$where['title']){
            unset($where['title']);
        }
        $data = $this->services->getList($where);
        if($data['count'] > 0){
            foreach ($data['list'] as $key => $item){
                unset($data['list'][$key]['type']);
                unset($data['list'][$key]['content']);
                unset($data['list'][$key]['sort']);
            }
        }
        return app('json')->success($data);
    }

    /**
     * 删除
     * @param int $id
     * @return mixed
     */
    public function delete($id)
    {
        if (!$id) return app('json')->fail(100100);
        $this->services->del($id);
        return app('json')->success(100002);
    }

    /**
     * 获取协议内容
     * @param $type
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAgreement($type)
    {
        if (!$type) return app('json')->fail(400184);
        $info = $this->services->getAgreementBytype($type);
        return app('json')->success($info);
    }

    /**
     * 保存协议内容
     * @return mixed
     */
    public function saveAgreement($id = 0)
    {
        $data = $this->request->postMore([
            ['id', $id],
            ['type', 0],
            ['title', ''],
            ['content', ''],
            ['file_id',0],
            ['admin_id',$this->adminId],
            ['admin_name',$this->adminInfo['real_name']],
        ]);
        $data['status'] = 1;
        $this->services->saveAgreement($data, $data['id']);
        return app('json')->success(100000);
    }

    /**
     * 保存协议内容
     * @return mixed
     */
    public function updateAgreement($id = 0)
    {
        $data = $this->request->postMore([
            ['id', $id],
            ['file_id', 0],
        ]);
        $data['status'] = 1;
        $data['add_time'] = time();
        $this->services->update($data['id'], $data);
        return app('json')->success(100000);
    }
}
